package com.decent.sms.controller;

import com.decent.sms.entity.IAcsBean;
import com.decent.sms.enums.SmsTypeEnum;
import com.decent.sms.service.SmsService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PhoneUtil;
import com.aliyuncs.exceptions.ClientException;
import com.decent.common.bo.SimpleMessage;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.common.exception.ErrorCodeException;
import com.decent.common.util.http.GetIpUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 短信服务管理
 *
 * @author wangyx
 */
@RequestMapping("sms")
@RestController
public class SmsController {
    @Resource
    private SmsService smsService;

    /**
     * 滑块验证码校验并发送短信
     *
     * @param telephone 手机号
     * @param type      操作类型
     * @param sessionId 滑动验证码必需参数
     * @param sig       滑动验证码必需参数
     * @param token     滑动验证码必需参数
     * @param scene     滑动验证码必需参数
     * @return 返回信息
     */
    @SuppressWarnings("JavadocReference")
    @RequestMapping("sendIAcsCode")
    public SimpleMessage sendIacsCode(String telephone, SmsTypeEnum type, IAcsBean bean) throws ClientException {
        String ip = GetIpUtil.getIp();
        if (ObjectUtil.hasNull(telephone, type, ip)) {
            throw new ErrorCodeException(ErrorCodeEnum.INVALID_PARAMS);
        }
        if (!PhoneUtil.isMobile(telephone)) {
            throw new ErrorCodeException(ErrorCodeEnum.INVALID_PARAMS, "手机号格式不正确");
        }
        bean.paramVerify();
        return smsService.sendIacsCode(telephone, type, ip, bean);
    }
}
